From f6e775186019ee053fd29b67d736cc61ea1af7ae Mon Sep 17 00:00:00 2001 From: robertl Date: Fri, 20 Sep 2002 17:47:46 +0000 Subject: [PATCH] Better respect for waypoint const-ness. From Alex. --- gpsbabel/csv.c | 24 ++++++++----- gpsbabel/csv_util.c | 82 ++++++++++++++++++++++++--------------------- gpsbabel/csv_util.h | 6 ++-- gpsbabel/mxf.c | 42 +++++++++++++++-------- gpsbabel/ozi.c | 30 +++++++++++------ gpsbabel/psp.c | 49 +++++++++++++++++---------- 6 files changed, 140 insertions(+), 93 deletions(-) diff --git a/gpsbabel/csv.c b/gpsbabel/csv.c index e2b74404f..f7c70cd32 100644 --- a/gpsbabel/csv.c +++ b/gpsbabel/csv.c @@ -72,12 +72,12 @@ data_read(void) linecount++; memset(&buff, '\0', sizeof(buff)); fgets(buff, sizeof(buff), file_in); - + if (strlen(buff)) { wpt_tmp = xcalloc(sizeof(*wpt_tmp), 1); - s = buff; + /* data delimited by commas, not enclosed */ s = csv_lineparse(s, ",", "", linecount); @@ -92,8 +92,7 @@ data_read(void) wpt_tmp->position.longitude.degrees = atof(s); break; case 2: - wpt_tmp->description = xstrdup(s); - wpt_tmp->description = csv_stringtrim(wpt_tmp->description, " "); + wpt_tmp->description = csv_stringtrim(s, " "); break; default: fprintf (stderr, "%s: Warning: unmapped data fields on line %d.\n", @@ -106,6 +105,10 @@ data_read(void) } wpt_tmp->creation_time = time(NULL); + + /* We'll make up our own shortname. */ + wpt_tmp->shortname = mkshort(wpt_tmp->description); + waypt_add(wpt_tmp); } else { @@ -116,30 +119,33 @@ data_read(void) } static void -gpsutil_disp(waypoint *wpt) +csv_waypt_pr(const waypoint *wpt) { double lon,lat; + char * description = NULL; lon = wpt->position.longitude.degrees; lat = wpt->position.latitude.degrees; if (wpt->description) - wpt->description = csv_stringclean(wpt->description, ",\""); + description = csv_stringclean(wpt->description, ",\""); fprintf(file_out, "%08.5f, %08.5f, %s\n", lat, lon, - wpt->description); + description); + + if (description) + free (description); } static void data_write(void) { - waypt_disp_all(gpsutil_disp); + waypt_disp_all(csv_waypt_pr); } - ff_vecs_t csv_vecs = { rd_init, wr_init, diff --git a/gpsbabel/csv_util.c b/gpsbabel/csv_util.c index fe0b440b0..8f27c5f2f 100644 --- a/gpsbabel/csv_util.c +++ b/gpsbabel/csv_util.c @@ -28,55 +28,58 @@ /*********************************************************************/ /* csv_stringclean() - remove any unwanted characters from string. */ -/* returns MODIFIED string. */ -/* usage: csv_stringclean(stringtoclean, "&,\"") */ +/* returns copy of string. */ +/* usage: p = csv_stringclean(stringtoclean, "&,\"") */ /* (strip out ampersands, commas, and quotes. */ /*********************************************************************/ char * -csv_stringclean(char *string, const char *chararray) { - char * p; - char * lp; +csv_stringclean(const char *string, const char *chararray) { + char * p1; + char * p2; const char * cp; + char * tmp = xstrdup(string); if ((! string) || (! chararray)) { - return (string); + return (tmp); } - - /* lp - end of the original string */ - lp = string; - while (*lp) lp++; + + /* p2 - end of the original string */ + p2 = tmp; + while (*p2) p2++; cp = chararray; while (*cp) { - p = string; - while (*p) { - if (*cp == *p) { + p1 = tmp; + while (*p1) { + if (*cp == *p1) { /* we don't want this character! */ - strncpy(p, p+1, (lp - p)); + strncpy(p1, p1 + 1, (p2 - p1)); + p1[p2 - p1] = '\0'; } - p++; + p1++; } cp++; } - return (string); + return (tmp); } /***********************************************************************************/ /* csv_stringtrim() - trim whitespace and leading and trailing enclosures (quotes) */ -/* returns MODIFIED string. */ -/* usage: csv_stringtrim(string, "\"") */ +/* returns a copy of the modified string */ +/* usage: p = csv_stringtrim(string, "\"") */ /***********************************************************************************/ char * -csv_stringtrim(char *string, const char *enclosure) +csv_stringtrim(const char *string, const char *enclosure) { - static char *p1 = NULL; + static const char *p1 = NULL; char *p2 = NULL; + char * tmp = xstrdup(string); size_t elen; - if (!string) { - return (string); + if (!strlen(string)) { + return (tmp); } if (!enclosure) { @@ -85,53 +88,53 @@ csv_stringtrim(char *string, const char *enclosure) elen = strlen(enclosure); } - p2 = string; + p2 = tmp; + p1 = tmp; - /* advance pointer to the end of the string */ - while ((*p2) && (p2++)) { - } + /* advance p2 to the end of the string, then back it off. */ + while ((*p2) && (p2++)) { } p2--; /* trim off trailing whitespace */ while (isspace(*p2)) { - *p2 = '\0'; p2--; } - p1 = string; - /* advance p1 past any leading whitespace */ while (isspace(*p1)) { p1++; } - /* if we have enclosures, yank them out in pairs */ + /* if we have enclosures, skip past them in pairs */ if (elen) { - while ((strncmp(p1, enclosure, elen) == 0) + while ((strncmp(tmp, enclosure, elen) == 0) && (strncmp(p2, enclosure, elen) == 0)) { - *p2 = '\0'; - p2--; - p1++; + p2 -= elen; + p1 += elen; } } - return (p1); + /* copy what's left over back into tmp. */ + strncpy(tmp, p1, (p2 - p1) + 1); + tmp[(p2 - p1) + 1] = '\0'; + + return (tmp); } /*****************************************************************************/ /* csv_lineparse() - extract data fields from a delimited string. designed */ /* to handle quoted and delimited data within quotes. */ /* returns temporary COPY of delimited data field (use it */ -/* or lose it). */ +/* or lose it on the next call). */ /* usage: p = csv_lineparse(string, ",", "\"", line) [initial call] */ /* p = csv_lineparse(NULL, ",", "\"", line) [subsequent calls] */ /*****************************************************************************/ char * -csv_lineparse(char *stringstart, const char *delimited_by, +csv_lineparse(const char *stringstart, const char *delimited_by, const char *enclosed_in, const int line_no) { - char *sp; - static char *p = NULL; + const char *sp; + static const char *p = NULL; static char *tmp = NULL; size_t dlen, elen; int enclosedepth = 0; @@ -181,6 +184,7 @@ csv_lineparse(char *stringstart, const char *delimited_by, tmp = xcalloc((p - sp) + 1, sizeof(char)); strncpy(tmp, sp, (p - sp)); + tmp[p - sp] = '\0'; if (dfound) { /* skip over the delimited_by */ diff --git a/gpsbabel/csv_util.h b/gpsbabel/csv_util.h index 170cc43b5..5d2660bf8 100644 --- a/gpsbabel/csv_util.h +++ b/gpsbabel/csv_util.h @@ -19,10 +19,10 @@ /* function prototypes */ char * -csv_stringtrim(char *string, const char *enclosure); +csv_stringtrim(const char *string, const char *enclosure); char * -csv_lineparse(char *stringstart, const char *delimited_by, const char *enclosed_in, const int line_no); +csv_lineparse(const char *stringstart, const char *delimited_by, const char *enclosed_in, const int line_no); char * -csv_stringclean(char *string, const char *chararray); +csv_stringclean(const char *string, const char *chararray); diff --git a/gpsbabel/mxf.c b/gpsbabel/mxf.c index ca062349e..e5399b467 100644 --- a/gpsbabel/mxf.c +++ b/gpsbabel/mxf.c @@ -87,8 +87,9 @@ data_read(void) /* data delimited by commas, possibly enclosed in quotes. */ s = buff; - s = csv_lineparse(s, ",", "\"", linecount); + s = csv_lineparse(s, ",", "\"", linecount); + i = 0; while (s) { switch (i) { @@ -99,12 +100,10 @@ data_read(void) wpt_tmp->position.longitude.degrees = atof(s); break; case 2: - wpt_tmp->description = xstrdup(s); - wpt_tmp->description = csv_stringtrim(wpt_tmp->description, ""); + wpt_tmp->description = csv_stringtrim(s, ""); break; case 3: - wpt_tmp->shortname = xstrdup(s); - wpt_tmp->shortname = csv_stringtrim(wpt_tmp->shortname, ""); + wpt_tmp->shortname = csv_stringtrim(s, ""); break; case 4: /* ignore. another name-type */ @@ -142,27 +141,42 @@ data_read(void) } static void -mxf_disp(waypoint * wpt) +mxf_waypt_pr(const waypoint * wpt) { int icon = 47; /* default to "dot" */ const char *color_hex = "ff0000"; - - csv_stringclean(wpt->shortname, ",\""); - wpt->shortname = csv_stringtrim(wpt->shortname, ""); + char *shortname = NULL; + char *description = NULL; + + if (wpt->shortname) { + shortname = csv_stringclean(wpt->shortname, ",\""); + shortname = csv_stringtrim(shortname, ""); + } else { + shortname = xstrdup(""); + } + + if (wpt->description) { + description = csv_stringclean(wpt->description, ",\""); + description = csv_stringtrim(description, ""); + } else { + shortname = xstrdup(""); + } - csv_stringclean(wpt->description, ",\""); - wpt->description = csv_stringtrim(wpt->description, ""); - fprintf(file_out, "%08.5f, %08.5f, \"%s\", \"%s\", \"%s\", %s, %d\n", wpt->position.latitude.degrees, wpt->position.longitude.degrees, - wpt->description, wpt->shortname, wpt->description, + description, shortname, description, color_hex, icon); + + if (description) + free(description); + if (shortname) + free(shortname); } static void data_write(void) { - waypt_disp_all(mxf_disp); + waypt_disp_all(mxf_waypt_pr); } ff_vecs_t mxf_vecs = { diff --git a/gpsbabel/ozi.c b/gpsbabel/ozi.c index e64a69a39..7fb507314 100644 --- a/gpsbabel/ozi.c +++ b/gpsbabel/ozi.c @@ -95,8 +95,7 @@ data_read(void) break; case 1: /* waypoint name */ - wpt_tmp->shortname = xstrdup(s); - wpt_tmp->shortname = csv_stringtrim(wpt_tmp->shortname, ""); + wpt_tmp->shortname = csv_stringtrim(s, ""); break; case 2: /* degrees latitude */ @@ -127,8 +126,7 @@ data_read(void) break; case 10: /* Description */ - wpt_tmp->description = xstrdup(s); - wpt_tmp->description = csv_stringtrim(wpt_tmp->description, ""); + wpt_tmp->description = csv_stringtrim(s, ""); break; case 11: @@ -174,24 +172,36 @@ data_read(void) } static void -ozi_disp(waypoint * wpt) +ozi_waypt_pr(const waypoint * wpt) { static int index = 0; double alt_feet; double ozi_time; + char * description; + char * shortname; ozi_time = (wpt->creation_time / 86400.0) + 25569.0; alt_feet = (wpt->position.altitude.altitude_meters * 3.2808); - csv_stringclean(wpt->description, ","); - csv_stringclean(wpt->shortname, ","); + if (wpt->description) + description = csv_stringclean(wpt->description, ","); + else + description = xstrdup(""); + + if (wpt->shortname) + shortname = csv_stringclean(wpt->shortname, ","); + else + shortname = xstrdup(""); index++; fprintf(file_out, "%4d,%-14.14s,%11.6f,%11.6f,%011.5f,%3d,%2d,%2d,%10d,%10d,%-40.40s,%2d,%2d,%5d,%7.0f,%2d,%2d,%2d\n", - index, wpt->shortname, wpt->position.latitude.degrees, + index, shortname, wpt->position.latitude.degrees, wpt->position.longitude.degrees, ozi_time, 0, 1, 3, 0, 65535, - wpt->description, 0, 0, 0, alt_feet, 6, 0, 17); + description, 0, 0, 0, alt_feet, 6, 0, 17); + + free(description); + free(shortname); } @@ -204,7 +214,7 @@ data_write(void) fprintf(file_out, "Reserved 2\n"); fprintf(file_out, "Reserved 3\n"); - waypt_disp_all(ozi_disp); + waypt_disp_all(ozi_waypt_pr); } ff_vecs_t ozi_vecs = { diff --git a/gpsbabel/psp.c b/gpsbabel/psp.c index 32941e394..73c5bce78 100644 --- a/gpsbabel/psp.c +++ b/gpsbabel/psp.c @@ -233,24 +233,34 @@ psp_read(void) } static void -psp_disp(waypoint *wpt) +psp_waypt_pr(const waypoint *wpt) { double lon, lat; char tbuf[64]; char c; int i; - - /* this output format pretty much requires a description and a shortname */ - if (!wpt->shortname) { - if (wpt->description) - wpt->shortname = xstrdup(wpt->description); + char *shortname; + char *description; + + + /* this output format pretty much requires a description + * and a shortname + */ + + if (wpt->shortname) { + shortname = xstrdup(wpt->shortname); + } else { + if (wpt->description) + shortname = xstrdup(wpt->description); else - wpt->shortname = xstrdup(""); - } - if (!wpt->description) { - wpt->description = xstrdup(wpt->shortname); + shortname = xstrdup(""); } - + + if (wpt->description) { + description = xstrdup(wpt->description); + } else { + description = xstrdup(shortname); + } /* convert lat/long back to radians */ lat = (wpt->position.latitude.degrees * M_PI) / 180.0; @@ -288,21 +298,21 @@ psp_disp(waypoint *wpt) /* 3 unknown bytes */ fwrite(&tbuf, 1, 3, psp_file_out); /* 3 junk */ - c = strlen(wpt->shortname); + c = strlen(shortname); /* 1 string size */ fwrite(&c, 1, 1, psp_file_out); - for (i = 0 ; wpt->shortname[i] ; i++) { - fwrite(&wpt->shortname[i], 1, 1, psp_file_out); /* char */ + for (i = 0 ; shortname[i] ; i++) { + fwrite(&shortname[i], 1, 1, psp_file_out); /* char */ fwrite(&tbuf[0], 1, 1, psp_file_out); /* null */ } - c = strlen(wpt->description); + c = strlen(description); /* 1 byte string size */ fwrite(&c, 1, 1, psp_file_out); - for (i = 0 ; wpt->description[i] ; i++) { - fwrite(&wpt->description[i], 1, 1, psp_file_out); /* char */ + for (i = 0 ; description[i] ; i++) { + fwrite(&description[i], 1, 1, psp_file_out); /* char */ fwrite(&tbuf[0], 1, 1, psp_file_out); /* null */ } @@ -315,6 +325,9 @@ psp_disp(waypoint *wpt) fwrite(&tbuf[i], 1, 1, psp_file_out); /* char */ fwrite(&tbuf[0], 1, 1, psp_file_out); /* null */ } + + free (shortname); + free (description); } static void @@ -338,7 +351,7 @@ psp_write(void) fwrite(&header_bytes, 1, 32, psp_file_out); - waypt_disp_all(psp_disp); + waypt_disp_all(psp_waypt_pr); } ff_vecs_t psp_vecs = { -- 2.30.2